package com.eascs.ticket.user.service;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.eascs.common.assertion.util.AssertInfoUtils;
import com.eascs.ticket.common.service.BaseSevice;
import com.eascs.ticket.user.entity.TeamItem;

@Service
public class TeamItemService extends BaseSevice {

	public List<Object[]> getTeamItemViewByTeamCode(String teamCode) {
		return this.proxyDao.findObjectsList("select id,item_user_code from team_item where team_code=? ", teamCode);
	}

	@Transactional(transactionManager = "ticketTransactionManager")
	public void insertTeamItem(TeamItem teamItem) {
		TeamItem teamItemDb = new TeamItem();
		teamItemDb.setItemUserCode(teamItem.getItemUserCode());
		List<TeamItem> teamItems = this.proxyDao.findEntitysWithAny(teamItemDb);
		AssertInfoUtils.assertTrue(teamItems.isEmpty() || (teamItems.size() == 1 && teamItems.get(0).getTeamCode().equals(teamItem.getTeamCode())), "一个人只能为一个团队的成员！");
		if (teamItems.isEmpty()) {
			this.proxyDao.insert(teamItem);
		}
	}

	@Transactional(transactionManager = "ticketTransactionManager")
	public void deleteTeamItem(String id) {
		this.proxyDao.deleteById(TeamItem.class, id);
	}

	@Transactional(transactionManager = "ticketTransactionManager")
	public void deleteTeamItemByTeamCode(String teamCode) {
		TeamItem teamItem = new TeamItem();
		teamItem.setTeamCode(teamCode);
		this.proxyDao.deleteEntitysWithAny(teamItem);
	}
}
